/**
 * 
 */
package us.gaaoc.framework.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;

import us.gaaoc.framework.model.Court;
import us.gaaoc.framework.model.DocumentInstance;

/**
 * 
 * @author hea May 18, 2007
 */
public class DocumentInstanceDAOHibernate extends
		GenericHibernateDAO<DocumentInstance, Integer, DocumentInstanceDAO>
		implements DocumentInstanceDAO {

	/**
	 * Method to query list of document Instances by code and description
	 * 
	 * returns List of Select Items filled with Document Instance ID's and
	 * descriptions
	 */
	@SuppressWarnings("unchecked")
	public List<DocumentInstance> findDocumentInstanceByAliasType() {
		// Create a Query and initialize it
		LOG.debug("Inside getting DocumentTypesByAlias");
		ArrayList<DocumentInstance> documentInstanceList = new ArrayList<DocumentInstance>();
		Query q = this.getSession().createQuery(
				"from DocumentInstance di  order by description");
		LOG.debug("Executed Query . . .");
		// Put the results of the query into Document Type Structures
		List l = q.list();
		Iterator i = l.iterator();
		while (i.hasNext()) {
			documentInstanceList.add((DocumentInstance) i.next());
		}
		return documentInstanceList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * us.gaaoc.framework.dao.DocumentInstanceDAO#findDocumentInstanceByAliasType
	 * (us.gaaoc.framework.model.Court)
	 */
	@SuppressWarnings("unchecked")
	public List<DocumentInstance> findDocumentInstanceByAliasType(Court court) {
		LOG.debug("Inside getting DocumentTypesByAlias");
		ArrayList<DocumentInstance> documentInstanceList = new ArrayList<DocumentInstance>();

		// Query q = this.getSession().createQuery(
		// "from DocumentInstance di where di.court=? order by description");
		Query q = getSession()
				.createQuery(
						"from DocumentInstance di where di.court=? order by description");
		q.setEntity(0, court);
		// Put the results of the query into Document Type Structures
		List l = q.list();
		LOG.debug("Executed Query . . .");
		Iterator i = l.iterator();
		while (i.hasNext()) {
			documentInstanceList.add((DocumentInstance) i.next());
		}
		return documentInstanceList;
	}

	public DocumentInstance findGenericDocumentInstance(String localCode,
			Court court) {
		DocumentInstance documentInstance = null;
		String sql = "from DocumentInstance di where di.court = ? and di.docLocalCode.code = ? order by di.code asc";
		Query q = this.getSession().createQuery(sql);
		q.setEntity(0, court);
		q.setString(1, localCode);
		List result = q.list();
		if (result != null && !result.isEmpty()) {
			documentInstance = (DocumentInstance) result.get(0);
		}
		return documentInstance;
	}

	@Override
	public List<DocumentInstance> findAllDocumentsByCourt(String courtUuid) {
		List<DocumentInstance> l = new ArrayList<DocumentInstance>();

		Query q = getSession().createQuery(
				"from DocumentInstance where courtUuid = :uuid").setParameter(
				"uuid", courtUuid);

		List list = q.list();

		for (Object o : list) {
			l.add((DocumentInstance) o);
		}

		return l;
	}

}
